//
//  ZYComposePictureView.swift
//  WBBeta1
//
//  Created by wustzhy on 16/8/17.
//  Copyright © 2016年 Yestin. All rights reserved.
//

import UIKit

let composePictureCellId = "composePictureCellId"
class ZYComposePictureView: UICollectionView {
    
    var imageList:[UIImage] = [UIImage]()
    var closureSelectCell:(()->())?
    // MARK: - 供外界调用 
    func addImage(image:UIImage){
        self.hidden = false
        imageList.append(image)
        reloadData()//刷新
    }
    
    override init(frame: CGRect, collectionViewLayout layout: UICollectionViewLayout) {
        super.init(frame: frame, collectionViewLayout: UICollectionViewFlowLayout())
        setuiUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    private func setuiUI() {
        //默认隐藏
        self.hidden = true
        backgroundColor = UIColor.whiteColor()
        //代理
        dataSource = self
        delegate = self
        //注册cell
        registerClass(ZYComposePictureViewCell.self, forCellWithReuseIdentifier: composePictureCellId)
        //设置flowlayout
        let flowLayout = self.collectionViewLayout as! UICollectionViewFlowLayout
        
        let cellMargin:CGFloat = 5
        let cellWH:CGFloat = (SCREENW-20-2*cellMargin)/3
        //设置itemSize
        flowLayout.itemSize = CGSize(width: cellWH, height: cellWH)
        //间距
        flowLayout.minimumInteritemSpacing = 0
        flowLayout.minimumLineSpacing = 0
    }

    
    
}

extension ZYComposePictureView:UICollectionViewDataSource,UICollectionViewDelegate {
    
    
    func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        let count = imageList.count
        if count == 0 || count == 9 {
            return count
        }
        return imageList.count+1

    }
    
    func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCellWithReuseIdentifier(composePictureCellId, forIndexPath: indexPath) as! ZYComposePictureViewCell
        cell.backgroundColor = UIColor.whiteColor()
//        printLog(indexPath.item)
        // 除了0 和 9
        if indexPath.item == imageList.count  {
            cell.image = nil
        }else {
            // 赋值
        cell.image = imageList[indexPath.item]
            //删图closure的实现
            cell.closureDeletePic = { [weak self] in
                self?.imageList.removeAtIndex(indexPath.item)
                if self?.imageList.count == 0 {
                    self?.hidden = true
                }
                //刷新
                self?.reloadData()
            }
        }
        return cell
    }
    
    func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) {
        // 最后一个cell 为 “+图”
        if indexPath.item == imageList.count {
            closureSelectCell?()
        }
    }
}


// 自定义 cell
class ZYComposePictureViewCell:UICollectionViewCell {

    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    func setupUI() {
//        backgroundColor = RandomColor()
        //添加控件
        contentView.addSubview(bgImgView)
        contentView.addSubview(deleteBtn)
        
        //添加 约束
        bgImgView.snp_makeConstraints { (make) -> Void in
            make.edges.equalTo(contentView).offset(UIEdgeInsetsZero)
        }
        deleteBtn.snp_makeConstraints { (make) -> Void in
            make.top.trailing.equalTo(contentView)
        }
        
    }
    var image:UIImage? {
        didSet{
            if image == nil {
                bgImgView.image = UIImage(named: "compose_pic_add")
                bgImgView.highlightedImage = UIImage(named: "compose_pic_add_highlighted")
                deleteBtn.hidden = true
            }else {
                bgImgView.image = image
                //防止重用 点击显示
                bgImgView.highlightedImage = nil
                deleteBtn.hidden = false
            }
        }
    }
    //cell图片
    private lazy var bgImgView:UIImageView = UIImageView()
    //删除按钮
    private lazy var deleteBtn:UIButton = {
       let btn = UIButton()
        btn.addTarget(self, action: "deletePicture", forControlEvents: UIControlEvents.TouchUpInside)
        btn.setImage(UIImage(named: "compose_photo_close"), forState: UIControlState.Normal)
        return btn
    }()
    //点击删除 调用方法
    @objc private func deletePicture() {
        closureDeletePic?()
    }
    //删除图片的闭包
    var closureDeletePic: (()->())?
    
}











